package com.example.handle;

import cn.hutool.core.util.ObjectUtil;
import com.event.cache.iface.ICacheCleanHandle;
import com.example.service.IRedisService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Description CacheCleanHandle
 * @Author Bing
 * @Date 2025/1/21 14:26
 */
@Component
public class CacheCleanHandle implements ICacheCleanHandle {

    @Resource
    private IRedisService redisService;

    @Override
    public List<String> scanKeys(String pattern, int count) {
        return redisService.scanKeys(pattern, count);
    }

    @Override
    public void clean(List<String> keys) {
        redisService.delKey(keys);
    }

    @Override
    public void addCleanQueue(String key, String value) {
        redisService.leftPush(key, value);
    }

    @Override
    public void consumeCleanQueue(String key, Consumer<String> valueConsumer) {
        Object object = redisService.rightPop(key);
        if(ObjectUtil.isNull(object)){
            return;
        }
        valueConsumer.accept(object.toString());
    }
}